package org.dromara.datacheck.service.impl.jd;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.datacheck.domain.bo.jd.JdPurchaseEnterWarehouseOrderBo;
import org.dromara.datacheck.domain.jd.JdPurchaseEnterWarehouseOrder;
import org.dromara.datacheck.domain.vo.jd.JdPurchaseEnterWarehouseOrderVo;
import org.dromara.datacheck.mapper.jd.JdPurchaseEnterWarehouseOrderMapper;
import org.dromara.datacheck.service.jd.IJdPurchaseEnterWarehouseOrderService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 金蝶-采购入库单Service业务层处理
 *
 * @author 刘武贵
 * @date 2024-09-08
 */
@RequiredArgsConstructor
@Service
public class JdPurchaseEnterWarehouseOrderServiceImpl implements IJdPurchaseEnterWarehouseOrderService {

    private final JdPurchaseEnterWarehouseOrderMapper baseMapper;

    /**
     * 查询金蝶-采购入库单
     *
     * @param id 主键
     * @return 金蝶-采购入库单
     */
    @Override
    public JdPurchaseEnterWarehouseOrderVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询金蝶-采购入库单列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 金蝶-采购入库单分页列表
     */
    @Override
    public TableDataInfo<JdPurchaseEnterWarehouseOrderVo> queryPageList(JdPurchaseEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JdPurchaseEnterWarehouseOrder> lqw = buildQueryWrapper(bo);
        Page<JdPurchaseEnterWarehouseOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的金蝶-采购入库单列表
     *
     * @param bo 查询条件
     * @return 金蝶-采购入库单列表
     */
    @Override
    public List<JdPurchaseEnterWarehouseOrderVo> queryList(JdPurchaseEnterWarehouseOrderBo bo) {
        LambdaQueryWrapper<JdPurchaseEnterWarehouseOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 构建查询包装器
     *
     * @param bo 入库订单业务对象，包含查询参数
     * @return LambdaQueryWrapper 查询包装器，用于后续的条件查询
     */
    private LambdaQueryWrapper<JdPurchaseEnterWarehouseOrder> buildQueryWrapper(JdPurchaseEnterWarehouseOrderBo bo) {
        // 获取可能的额外查询参数
        Map<String, Object> params = bo.getParams();

        // 初始化LambdaQueryWrapper，用于构建查询条件
        LambdaQueryWrapper<JdPurchaseEnterWarehouseOrder> lqw = Wrappers.lambdaQuery();

        // 添加采购入库单号相等的查询条件
        lqw.eq(StringUtils.isNotBlank(bo.getPurchaseEnterNumber()), JdPurchaseEnterWarehouseOrder::getPurchaseEnterNumber, bo.getPurchaseEnterNumber());
        // 添加入库日期相等的查询条件
        lqw.eq(StringUtils.isNotBlank(bo.getEnterWarehouseDate()), JdPurchaseEnterWarehouseOrder::getEnterWarehouseDate, bo.getEnterWarehouseDate());
        // 添加供应商名称包含的查询条件
        lqw.like(StringUtils.isNotBlank(bo.getSupplierName()), JdPurchaseEnterWarehouseOrder::getSupplierName, bo.getSupplierName());
        // 添加收货状态相等的查询条件
        lqw.eq(StringUtils.isNotBlank(bo.getReceiptStatus()), JdPurchaseEnterWarehouseOrder::getReceiptStatus, bo.getReceiptStatus());
        // 添加物料编码相等的查询条件
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialCode()), JdPurchaseEnterWarehouseOrder::getMaterialCode, bo.getMaterialCode());
        // 添加物料名称包含的查询条件
        lqw.like(StringUtils.isNotBlank(bo.getMaterialName()), JdPurchaseEnterWarehouseOrder::getMaterialName, bo.getMaterialName());
        // 添加仓库单位名称包含的查询条件
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseUnitName()), JdPurchaseEnterWarehouseOrder::getWarehouseUnitName, bo.getWarehouseUnitName());
        // 添加实际数量相等的查询条件
        lqw.eq(bo.getActualQuantity() != null, JdPurchaseEnterWarehouseOrder::getActualQuantity, bo.getActualQuantity());
        // 添加仓库名称包含的查询条件
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseName()), JdPurchaseEnterWarehouseOrder::getWarehouseName, bo.getWarehouseName());
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            lqw.between(JdPurchaseEnterWarehouseOrder::getNewOrderDate,
                params.get("beginTime"),
                params.get("endTime"));
        }
        // 返回构建好的查询包装器
        return lqw;
    }


    /**
     * 新增金蝶-采购入库单
     *
     * @param bo 金蝶-采购入库单
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(JdPurchaseEnterWarehouseOrderBo bo) {
        JdPurchaseEnterWarehouseOrder add = MapstructUtils.convert(bo, JdPurchaseEnterWarehouseOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改金蝶-采购入库单
     *
     * @param bo 金蝶-采购入库单
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(JdPurchaseEnterWarehouseOrderBo bo) {
        JdPurchaseEnterWarehouseOrder update = MapstructUtils.convert(bo, JdPurchaseEnterWarehouseOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JdPurchaseEnterWarehouseOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除金蝶-采购入库单信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 查询金蝶-采购入库单列表所有数据
     *
     * @return 金蝶-采购入库单列表
     */
    @Override
    public List<JdPurchaseEnterWarehouseOrder> queryAll() {
        return baseMapper.selectList(null);
    }

    /**
     * 清空金蝶-采购入库单列表所有数据
     *
     * @return 金蝶-采购入库单列表
     */
    @Override
    public Integer deleteAll() {
        return baseMapper.delete(new QueryWrapper<>());
    }

    /**
     * 分页查询金蝶-采购入库单列表
     * 业务查询条件：根据warhouseName仓库名称为东莞仓和国内在途仓查询
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 金蝶-采购入库单分页列表
     */
    @Override
    public TableDataInfo<JdPurchaseEnterWarehouseOrderVo> queryPageListByDongGuanAndGuoNei(JdPurchaseEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JdPurchaseEnterWarehouseOrder> lqw = buildQueryWrapperByDongGuanAndGuoNei(bo);
        Page<JdPurchaseEnterWarehouseOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    private LambdaQueryWrapper<JdPurchaseEnterWarehouseOrder> buildQueryWrapperByDongGuanAndGuoNei(JdPurchaseEnterWarehouseOrderBo bo) {
        // 获取可能的额外查询参数
        Map<String, Object> params = bo.getParams();

        // 初始化LambdaQueryWrapper，用于构建查询条件
        LambdaQueryWrapper<JdPurchaseEnterWarehouseOrder> lqw = Wrappers.lambdaQuery();

        // 添加采购入库单号相等的查询条件
        lqw.eq(StringUtils.isNotBlank(bo.getPurchaseEnterNumber()), JdPurchaseEnterWarehouseOrder::getPurchaseEnterNumber, bo.getPurchaseEnterNumber());
        // 添加入库日期相等的查询条件
        lqw.eq(StringUtils.isNotBlank(bo.getEnterWarehouseDate()), JdPurchaseEnterWarehouseOrder::getEnterWarehouseDate, bo.getEnterWarehouseDate());
        // 添加供应商名称包含的查询条件
        lqw.like(StringUtils.isNotBlank(bo.getSupplierName()), JdPurchaseEnterWarehouseOrder::getSupplierName, bo.getSupplierName());
        // 添加收货状态相等的查询条件
        lqw.eq(StringUtils.isNotBlank(bo.getReceiptStatus()), JdPurchaseEnterWarehouseOrder::getReceiptStatus, bo.getReceiptStatus());
        // 添加物料编码相等的查询条件
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialCode()), JdPurchaseEnterWarehouseOrder::getMaterialCode, bo.getMaterialCode());
        // 添加物料名称包含的查询条件
        lqw.like(StringUtils.isNotBlank(bo.getMaterialName()), JdPurchaseEnterWarehouseOrder::getMaterialName, bo.getMaterialName());
        // 添加仓库单位名称包含的查询条件
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseUnitName()), JdPurchaseEnterWarehouseOrder::getWarehouseUnitName, bo.getWarehouseUnitName());
        // 添加实际数量相等的查询条件
        lqw.eq(bo.getActualQuantity() != null, JdPurchaseEnterWarehouseOrder::getActualQuantity, bo.getActualQuantity());
        // 添加仓库名称包含的查询条件
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseName()), JdPurchaseEnterWarehouseOrder::getWarehouseName, bo.getWarehouseName());
        // 添加仓库名称包含的查询条件
        lqw.in(CollUtil.isNotEmpty(bo.getWarhouseNameList()), JdPurchaseEnterWarehouseOrder::getWarehouseName, bo.getWarhouseNameList());
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            lqw.between(JdPurchaseEnterWarehouseOrder::getNewOrderDate,
                params.get("beginTime"),
                params.get("endTime"));
        }
        lqw.eq(StringUtils.isNotBlank(bo.getReceiptStatus()), JdPurchaseEnterWarehouseOrder::getReceiptStatus, "已审核");
        return lqw;
    }


}
